package com.hnichr.ump.plan.controller;


import com.hnichr.ump.common.plan.entity.PlanSchedules;
import com.hnichr.ump.common.plan.service.PlanSchedulesService;
import com.hnichr.ump.common.plan.vo.PlanSchedulesVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan/schedule/v1/")
@Tag(name = "PlanSchedulesController", description = "重点工作计划接口")
public class PlanSchedulesController {

    @Autowired
    private PlanSchedulesService planSchedulesService;

    @GetMapping("list")
    @Operation(summary = "重点工作计划列表", description = "获取重点工作计划列表（无分页）", method = "GET")
    public Result<List<PlanSchedules>> list(@Parameter(description="查询参数") Criteria criteria){
        try{
            List<PlanSchedules> list = planSchedulesService.list(criteria);
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("page")
    @Operation(summary = "重点工作计划列表", description = "重点工作计划列表（分页）", method = "GET")
    public Result<Page<PlanSchedules>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        try{
            Pageable page = PageRequest.of(pageNo-1, pageSize);
            Page<PlanSchedules> list = planSchedulesService.listPage(page, criteria);
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("listWithContents")
    @Operation(summary = "重点工作计划列表-带工作内容", description = "获取重点工作计划列表（无分页）-带工作内容", method = "GET")
    public Result<List<PlanSchedules>> listWithContents(@Parameter(description="查询参数") Criteria criteria){
        try{
            List<PlanSchedules> list = planSchedulesService.list(criteria);
            planSchedulesService.getContents(list);
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }

    }

    @GetMapping("pageWithContents")
    @Operation(summary = "重点工作计划列表-带工作内", description = "重点工作计划列表（分页）-带工作内容", method = "GET")
    public Result<Page<PlanSchedules>> pageWithContents(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        try{
            Pageable page = PageRequest.of(pageNo-1, pageSize);
            Page<PlanSchedules> list = planSchedulesService.listPage(page, criteria);
            planSchedulesService.getContents(list.getContent());
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取重点工作计划", description = "根据ID获取重点工作计划", method = "GET")
    public Result<PlanSchedules> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        PlanSchedules schedules = planSchedulesService.getById(id);
        return Result.OK(schedules);
    }

    @PostMapping("add")
    @Operation(summary = "新增重点工作计划", description = "新增重点工作计划", method = "POST")
    public Result<PlanSchedules> add(@RequestBody PlanSchedulesVo schedules) {
        try{
            PlanSchedules planSchedules = planSchedulesService.save(schedules);
            return Result.OK(planSchedules);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }

    }

    @PutMapping("edit")
    @Operation(summary = "编辑重点工作计划", description = "编辑重点工作计划", method = "PUT")
    public Result<PlanSchedules> edit(@RequestBody PlanSchedulesVo schedules) {
        try{
            PlanSchedules planSchedules = planSchedulesService.edit(schedules);
            return Result.OK(planSchedules);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除重点工作计划", description = "根据id删除重点工作计划", method = "DELETE")
    public Result<PlanSchedules> remove(@PathVariable(name = "ids", required = true) String ids) {
        planSchedulesService.remove(ids);
        return Result.OK();
    }
}
